<?php

namespace app\admin\controller\shop;

use app\admin\model\shop\OrderAction;
use app\common\controller\Backend;

/**
 * 退货单管理
 *
 * @icon fa fa-circle-o
 */
class OrderRefund extends Backend
{

    /**
     * OrderAftersales模型对象
     * @var \app\admin\model\shop\OrderRefund
     */
    protected $model = null;
    protected $relationSearch = true;
    protected $searchFields = 'id,OrderGoods.order_sn';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shop\OrderRefund;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit, $page, $alias, $bind) = $this->buildparams();
            $list = $this->model
                ->with(['User','Order'])
                //->alias(['order_goods' => 'OrderGoods'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        return parent::edit($ids);
    }


    public function pass($ids = null)
    {
        $row = $this->model->get($ids);
        $order = \app\admin\model\shop\Order::get($row->order_id);
        if(!$order)$this->error('订单不存在或被删除');
        //执行同步退款
//        \app\admin\model\shop\Order::refund($order->order_sn, $order->paytype, $row->refund);

        //已退款

        \app\admin\model\shop\OrderGoods::where('order_sn', $order['order_sn'])->update(['salestate' => 4]);

        //未发货状态下才减库存
        if ($order->shippingstate == 0) {
            \app\api\model\OrderGoods::setGoodsStocksInc($order->order_sn); //库存增
            \app\api\model\OrderGoods::setGoodsSalesDec($order->order_sn); //销量减
        }

        //记录操作
        OrderAction::push($order->order_sn, '同意订单退款', '管理员');

        //\app\api\model\OrderGoods::update(['salestate'=>4],['order_sn'=>$order['order_sn']]);


        $order->refundtime = time();
        $order->orderstate = 3;
        $order->salestate = 4;
        $res = $order->save();

        $row->status = 2;
        $res = $row->save();

        if ($res){
            $this->success('成功');
        } else {
            $this->error('失败');
        }

    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        $order = \app\admin\model\shop\Order::get($row->order_id);
        \app\admin\model\shop\OrderGoods::where('order_sn', $order['order_sn'])->update(['salestate' => 6]);
        //记录操作
        OrderAction::push($order->order_sn, '拒绝订单退款', '管理员');
        $order->salestate = 6;
        $order->orderstate = 0;

        $res = $order->save();

        $row->status = 3;
        $res = $row->save();

        if ($res){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
}
